/*
 * Copyright 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wec_wallet.ebo.com.api.safe.cleanmemory;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.annotation.NonNull;

import java.util.List;

import wec_wallet.ebo.com.api.safe.data.model.AppProcessInfo;
import wec_wallet.ebo.com.api.safe.tools.SafeUtils;


public class CleanRAMPresenter implements CleanRAMContract.Presenter, CleanRAMService.OnProcessActionListener {

    private final CleanRAMContract.View mTasksView;
    private CleanRAMService mCoreService;
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mCoreService
                    = ((CleanRAMService.ProcessServiceBinder) service).getService();
            mCoreService.setOnActionListener(CleanRAMPresenter.this);
            checkRAMStatus();

        }


        @Override
        public void onServiceDisconnected(ComponentName name) {
            mCoreService.setOnActionListener(null);
            mCoreService = null;
        }
    };



    public CleanRAMPresenter(@NonNull CleanRAMContract.View tasksView) {
        mTasksView = tasksView;
        mTasksView.setPresenter(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void startCleanService(Context context) {
        Intent intent = new Intent(context, CleanRAMService.class);
        context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void checkRAMStatus() {
        mTasksView.startCleanRAMAnime();
        mCoreService.scanRunProcess();
    }

    @Override
    public void cleanRAM() {
        mCoreService.cleanAllProcess();

    }

    @Override
    public void onScanStarted(Context context) {
        mTasksView.startCleanRAMAnime();
        mTasksView.showProgress(-1, "正在扫描...");

    }

    @Override
    public void onScanProgressUpdated(Context context, int current, int max, long memory, String processName) {
        mTasksView.showProgress(SafeUtils.getPercent(context), processName);
    }

    @Override
    public void onScanCompleted(Context context, List<AppProcessInfo> apps) {
        mTasksView.stopCleanRAMAnime();
        mTasksView.showProgress(-1, "扫描完毕");
    }

    @Override
    public void onCleanStarted(Context context) {

    }

    @Override
    public void onCleanCompleted(Context context, long cacheSize) {


    }
}
